package com.handydata.antifraud.common.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.handydata.antifraud.dao.RiskRuleDictlinfoBasedataDao;
import com.handydata.antifraud.entity.RuleInfoDo;
import com.handydata.antifraud.rule.RuleEngineInvokerFactory;
import com.handydata.antifraud.util.HttpUtil;
import com.handydata.antifraud.ws.EncryInfo;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

@Slf4j
@Getter
@Setter
@Component
public class UruleCheckRun implements Callable<Map<String, List<RuleInfoDo>>> {

    private RuleEngineInvokerFactory ruleEngineInvokerFactory;

    private RiskRuleDictlinfoBasedataDao riskRuleDictlinfoBasedataDao;

    private EncryInfo encryInfo;


    @SuppressWarnings("deprecation")
	@Override
    public Map<String, List<RuleInfoDo>> call() throws Exception {
        List<Map<String, String>> riskRuleList = riskRuleDictlinfoBasedataDao.queryOrderList();
        if(riskRuleList.isEmpty()){
             throw new Exception("规则校验表匹配不到此信息,请检查有无消息!");
        }
        ruleEngineInvokerFactory.init(riskRuleList);
        log.info("請求{}開始拿文件:",encryInfo.getResporityServerUrl()+encryInfo.getResporityJSONFile());
        List<String> content = IOUtils.readLines(HttpUtil.getFileStream(encryInfo.getResporityServerUrl()+encryInfo.getResporityJSONFile(),10000));
        return JSON.parseObject(content.get(0), new TypeReference<Map<String, List<RuleInfoDo>>>(){});
    }

    public UruleCheckRun(RuleEngineInvokerFactory ruleEngineInvokerFactory, RiskRuleDictlinfoBasedataDao riskRuleDictlinfoBasedataDao, EncryInfo encryInfo) {
        this.ruleEngineInvokerFactory = ruleEngineInvokerFactory;
        this.riskRuleDictlinfoBasedataDao = riskRuleDictlinfoBasedataDao;
        this.encryInfo = encryInfo;
    }
}
